import { useState, useEffect } from "react";
import { useModel } from "umi";
import { Form, Input, Select, Button, message, Space, Dropdown, Row, Col, Cascader } from "antd";
import { DownOutlined } from "@ant-design/icons";
import { useReactive } from "ahooks";

import useCreateInstitution from "../../hooks/institution/useCreateInstitution";
import useStaff from "../../hooks/staff/useStaff";
import useCheckInstitutionName from "../../hooks/institution/useCheckInstitutionName";

const AddInstitutionForm = ({ reloadData }) => {
    const { onCloseGlobalModal } = useModel("global.modal");

    const { loading: staffLoading, staffs } = useStaff({});

    const { checkInstitutionName } = useCheckInstitutionName();
    const { createInstitution, submitLoading } = useCreateInstitution();

    const [form] = Form.useForm();

    const state = useReactive({
        loading: false,
        maInfo: {},
        asInfo: {},
    });

    let onFinish = async (values) => {
        values.oiType = "项目组";
        values.maInfo = state.maInfo || {};
        values.asInfo = state.asInfo || {};

        let res = await createInstitution(values);

        if (res) {
            form.resetFields();
            message.success(`成功创建项目组信息`);
            reloadData();
            onCloseGlobalModal();
        }
    };

    const oiNameValidator = async (rule, value) => {
        if (value && validates.titleAndName(value) && value.length < 31) {
            let res = await checkInstitutionName({ oiName: value });
            if (res === null) return Promise.reject(`检查项目组名称出错`);
            return res > 0 ? Promise.reject(`项目组名称重复！`) : Promise.resolve();
        } else {
            return Promise.reject(`项目组名称只能输入中文、英文、数字，最多30个汉字`);
        }
    };

    return (
        <Form form={form} layout="vertical" onFinish={onFinish} style={{ padding: 10 }}>
            <Form.Item label={`项目组名称`} name="oiName" validateTrigger="onBlur" rules={[{ required: true, validator: oiNameValidator }]}>
                <Input placeholder="请输入" />
            </Form.Item>

            <Row gutter={10}>
                <Col span={12}>
                    <Form.Item label="负责人" name="maInfo" rules={[{ required: true }]}>
                        <Select
                            loading={staffLoading}
                            placeholder="请选择"
                            options={staffs}
                            fieldNames={{ label: "osName", value: "osUuid" }}
                            onChange={(value, option) => (state.maInfo = { osUuid: option.osUuid, osName: option.osName })}
                        />
                    </Form.Item>
                </Col>
                <Col span={12}>
                    <Form.Item label="管理人" name="asInfo">
                        <Select
                            loading={staffLoading}
                            placeholder="请选择"
                            options={staffs}
                            fieldNames={{ label: "osName", value: "osUuid" }}
                            onChange={(value, option) => (state.asInfo = { osUuid: option.osUuid, osName: option.osName })}
                        />
                    </Form.Item>
                </Col>
            </Row>

            <Form.Item label="职能说明" name="oiDesc">
                <Input.TextArea placeholder="请输入" />
            </Form.Item>

            <div style={{ marginTop: 20, textAlign: "center" }}>
                <Space>
                    <Button loading={submitLoading} type="primary" htmlType="submit" style={{ width: "100%" }}>
                        提交
                    </Button>
                    <Button loading={submitLoading} onClick={() => form.resetFields()}>
                        重置
                    </Button>
                </Space>
            </div>
        </Form>
    );
};

export default AddInstitutionForm;
